﻿/********************************************************************
 *                                        
 * 命名空间 ： Utility
 * 类    名 ： StringHelper.cs
 * 功能描述 ： 常用字符串帮助工具类
 * 处理流程 ：                             
 * 算    法 ：                            
 * 姓    名 ： 王超
 * 日    期 ： 
 * 修    改 ：	                           
 * 日    期 ：	                           
 * 错误编号 ：	                            
 * 备    注 ：	                           
 * 版    本 ： V1.0                           
 *                                        
 ********************************************************************/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;


namespace GreatB2C.Utility
{
    /// <summary>
    /// 常用字符串帮助工具类
    /// </summary>
    [Serializable]
    public class StringHelper
    {
        /// <summary>
        /// 删除不可见字符
        /// </summary>
        /// <param name="sourceString"></param>
        /// <returns></returns>
        public static string DeleteUnVisibleChar( string sourceString )
        {
            System.Text.StringBuilder sBuilder = new System.Text.StringBuilder( 131 );

            for( int i = 0 ; i < sourceString.Length ; i++ )
            {
                int Unicode = sourceString[ i ];
                if( Unicode >= 16 )
                {
                    sBuilder.Append( sourceString[ i ].ToString() );
                }
            }

            return sBuilder.ToString();
        }

        /// <summary>
        /// 获取数组元素的合并字符串
        /// </summary>
        /// <param name="stringArray"></param>
        /// <returns></returns>
        public static string GetArrayString( string[ ] stringArray )
        {
            string totalString = null;

            for( int i = 0 ; i < stringArray.Length ; i++ )
            {
                totalString = totalString + stringArray[ i ];
            }

            return totalString;
        }

        /// <summary>
        ///		获取某一字符串在字符串数组中出现的次数
        /// </summary>
        /// <param name="stringArray" type="string[]">
        ///     <para>
        ///         字符串数组
        ///     </para>
        /// </param>
        /// <param name="findString" type="string">
        ///     <para>
        ///         待查的字符串
        ///     </para>
        /// </param>
        /// <returns>
        ///     包含次数
        /// </returns>
        public static int GetStringCount( string[ ] stringArray, string findString )
        {
            int count = -1;

            string totalString = GetArrayString( stringArray );

            string subString = totalString;

            while( subString.IndexOf( findString ) >= 0 )
            {
                subString = totalString.Substring( subString.IndexOf( findString ) );
                count += 1;
            }

            return count;
        }

        /// <summary>
        ///     获取某一字符串在字符串中出现的次数
        /// </summary>
        /// <param name="stringArray" type="string">
        ///     <para>
        ///         原字符串
        ///     </para>
        /// </param>
        /// <param name="findString" type="string">
        ///     <para>
        ///         匹配字符串
        ///     </para>
        /// </param>
        /// <returns>
        ///     匹配字符串数量
        /// </returns>
        public static int GetStringCount( string sourceString, string findString )
        {
            int count = 0;
            int findStringLength = findString.Length;
            string subString = sourceString;

            while( subString.IndexOf( findString ) >= 0 )
            {
                subString = subString.Substring( subString.IndexOf( findString ) + findStringLength );
                count += 1;
            }
            return count;
        }

        /// <summary>
        /// 按字节数取出字符串的长度
        /// </summary>
        /// <param name="strTmp">要计算的字符串</param>
        /// <returns>字符串的字节数</returns>
        public static int GetByteCount( string strTmp )
        {
            int intCharCount = 0;
            for( int i = 0 ; i < strTmp.Length ; i++ )
            {
                if( System.Text.UTF8Encoding.UTF8.GetByteCount( strTmp.Substring( i, 1 ) ) == 3 )
                {
                    intCharCount = intCharCount + 2;
                }
                else
                {
                    intCharCount = intCharCount + 1;
                }
            }
            return intCharCount;
        }

        /// <summary>
        /// 改正sql语句中的转义字符
        /// </summary>
        public static string mashSQL( string str )
        {
            string str2;

            if( str == null )
            {
                str2 = "";
            }
            else
            {
                str = str.Replace( "\'", "'" );
                str2 = str;
            }
            return str2;
        }

        /// <summary>
        /// 替换sql语句中的有问题符号
        /// </summary>
        public static string ChkSQL( string str )
        {
            string str2;

            if( str == null )
            {
                str2 = "";
            }
            else
            {
                str = str.Replace( "'", "''" );
                str2 = str;
            }
            return str2;
        }

        /// <summary>
        /// 按字节数要在字符串的位置
        /// </summary>
        /// <param name="intIns">字符串的位置</param>
        /// <param name="strTmp">要计算的字符串</param>
        /// <returns>字节的位置</returns>
        public static int GetByteIndex( int intIns, string strTmp )
        {
            int intReIns = 0;
            if( strTmp.Trim() == "" )
            {
                return intIns;
            }
            for( int i = 0 ; i < strTmp.Length ; i++ )
            {
                if( System.Text.UTF8Encoding.UTF8.GetByteCount( strTmp.Substring( i, 1 ) ) == 3 )
                {
                    intReIns = intReIns + 2;
                }
                else
                {
                    intReIns = intReIns + 1;
                }
                if( intReIns >= intIns )
                {
                    intReIns = i + 1;
                    break;
                }
            }
            return intReIns;
        }

        /// <summary>
        /// 格式化字符串长度，超出部分显示省略号,区分汉字跟字母。汉字2个字节，字母数字一个字节
        /// </summary>
        /// <param name="str">要格式化得字符串</param>
        /// <param name="n">截取得字符长度</param>
        /// <returns>返回截取后得字符串</returns>
        public static string StringFormat( string str, int n )
        {
            string temp = string.Empty;
            if( System.Text.Encoding.Default.GetByteCount( str ) <= n )//如果长度比需要的长度n小,返回原字符串
            {
                return str;
            }
            else
            {
                int t = 0;
                char[] q = str.ToCharArray();
                for( int i = 0 ; i < q.Length && t < n ; i++ )
                {
                    if( ( int )q[ i ] >= 0x4E00 && ( int )q[ i ] <= 0x9FA5 )//是否汉字
                    {
                        temp += q[ i ];
                        t += 2;
                    }
                    else
                    {
                        temp += q[ i ];
                        t++;
                    }
                }
                return ( temp + "..." );
            }
        }

        #region 转换为数字


        public static string TransNum( int intNum )
        {
            string strFun = "";

            switch( intNum )
            {
                case 1:
                    strFun = "一";
                    break;
                case 2:
                    strFun = "二";
                    break;
                case 3:
                    strFun = "三";
                    break;
                case 4:
                    strFun = "四";
                    break;
                case 5:
                    strFun = "五";
                    break;
                case 6:
                    strFun = "六";
                    break;
                case 7:
                    strFun = "七";
                    break;
                case 8:
                    strFun = "八";
                    break;
                case 9:
                    strFun = "九";
                    break;
                default:
                    strFun = "0";
                    break;
            }

            return strFun;
        }

        public static string TransBracketNum( int intNum )
        {
            string strFun = "";

            switch( intNum )
            {
                case 1:
                    strFun = "⑴";
                    break;
                case 2:
                    strFun = "⑵";
                    break;
                case 3:
                    strFun = "⑶";
                    break;
                case 4:
                    strFun = "⑷";
                    break;
                case 5:
                    strFun = "⑸";
                    break;
                case 6:
                    strFun = "⑹";
                    break;
                case 7:
                    strFun = "⑺";
                    break;
                case 8:
                    strFun = "⑻";
                    break;
                case 9:
                    strFun = "㈨";
                    break;
                default:
                    strFun = "⑽";
                    break;
            }

            return strFun;
        }

        #endregion

        #region 数据库中与C#中的数据类型对照
        /// <summary> 
        /// SQL数据库中与C#中的数据类型对照 
        /// </summary> 
        /// <param name="type">类型名</param> 
        /// <returns></returns> 
        private string SQLChangeToCSharpType( string type )
        {
            string reval = string.Empty;

            switch( type.ToLower() )
            {
                case "smallint":
                    reval = "Int16";
                    break;
                case "int":
                    reval = "Int32";
                    break;
                case "bigint":
                    reval = "Int64";
                    break;
                case "nchar":
                case "ntext":
                case "nvarchar":
                case "text":
                case "varchar":
                    reval = "String";
                    break;
                case "binary":
                    reval = "System.Byte[]";
                    break;
                case "bit":
                    reval = "Boolean";
                    break;
                case "char":
                    reval = "Char";
                    break;
                case "timestamp":
                case "smalldatetime":
                case "datetime":
                    reval = "System.DateTime";
                    break;
                case "float":
                    reval = "System.Double";
                    break;
                case "varbinary":
                case "image":
                    reval = "System.Byte[]";
                    break;
                case "decimal":
                case "smallmoney":
                case "money":
                case "numeric":
                    reval = "System.Decimal";
                    break;
                case "real":
                    reval = "System.Single";
                    break;
                case "tinyint":
                    reval = "System.Byte";
                    break;
                case "uniqueidentifier":
                    reval = "System.Guid";
                    break;
                case "Variant":
                    reval = "Object";
                    break;
                default:
                    reval = "String";
                    break;
            }

            return reval;
        }
        #endregion

        #region 只对六位数字作转换


        public static string TransValues( decimal MoneyValue )
        {
            string[] MoneyValues = MoneyValue.ToString().Split( ".".ToCharArray() );
            int MoneyValues_len;//小写钱的长度 
            string MoneyTranStr = "";//转换后的大写钱 
            string MoneyStr = "";//经过转换后的大写钱字串 
            string tempStr = "";

            MoneyValues_len = MoneyValues[ 0 ].Length;

            for( int i = 0 ; i < MoneyValues_len ; i++ ) //将阿拉伯数字转换成汉字 
            {
                string stra = MoneyValues[ 0 ].Substring( i, 1 );
                switch( stra )
                {
                    case "1":
                        MoneyTranStr = "壹";
                        break;
                    case "2":
                        MoneyTranStr = "贰";
                        break;
                    case "3":
                        MoneyTranStr = "叁";
                        break;
                    case "4":
                        MoneyTranStr = "肆";
                        break;
                    case "5":
                        MoneyTranStr = "伍";
                        break;
                    case "6":
                        MoneyTranStr = "陆";
                        break;
                    case "7":
                        MoneyTranStr = "柒";
                        break;
                    case "8":
                        MoneyTranStr = "捌";
                        break;
                    case "9":
                        MoneyTranStr = "玖";
                        break;
                    default:
                        MoneyTranStr = "零";
                        break;
                }

                MoneyStr = MoneyStr + MoneyTranStr;
            }

            switch( MoneyStr.Length.ToString() )//将汉字用单位连接起来,如贰贰 转换为 贰拾贰元 
            {
                case "1":
                    tempStr = MoneyStr + "元";
                    break;
                case "2":
                    tempStr = MoneyStr.Substring( 0, 1 ) + "拾" + ( MoneyStr.Substring( 1, 1 ) == "零" ? "" : MoneyStr.Substring( 1, 1 ) ) + "元";
                    break;
                case "3":
                    tempStr = MoneyStr.Substring( 0, 1 ) + "佰" + ( MoneyStr.Substring( 1, 1 ) == "零" ? "零" : MoneyStr.Substring( 1, 1 ) + "拾" ) + ( MoneyStr.Substring( 2, 1 ) == "零" ? "" : MoneyStr.Substring( 2, 1 ) ) + "元";
                    break;
                case "4":
                    tempStr = MoneyStr.Substring( 0, 1 ) + "仟" + ( MoneyStr.Substring( 1, 1 ) == "零" ? "零" : MoneyStr.Substring( 1, 1 ) + "佰" ) + ( MoneyStr.Substring( 2, 1 ) == "零" ? "零" : MoneyStr.Substring( 2, 1 ) + "拾" ) + ( MoneyStr.Substring( 3, 1 ) == "零" ? "" : MoneyStr.Substring( 3, 1 ) ) + "元";
                    break;
                case "5":
                    tempStr = MoneyStr.Substring( 0, 1 ) + "万" + ( MoneyStr.Substring( 1, 1 ) == "零" ? "零" : MoneyStr.Substring( 1, 1 ) + "仟" ) + ( MoneyStr.Substring( 2, 1 ) == "零" ? "零" : MoneyStr.Substring( 2, 1 ) + "佰" ) + ( MoneyStr.Substring( 3, 1 ) == "零" ? "零" : MoneyStr.Substring( 3, 1 ) + "拾" ) + ( MoneyStr.Substring( 4, 1 ) == "零" ? "" : MoneyStr.Substring( 4, 1 ) ) + "元";
                    break;
                case "6":
                    tempStr = MoneyStr.Substring( 0, 1 ) + "拾" + ( MoneyStr.Substring( 1, 1 ) == "零" ? "万" : MoneyStr.Substring( 1, 1 ) + "万" ) + ( MoneyStr.Substring( 2, 1 ) == "零" ? "零" : MoneyStr.Substring( 2, 1 ) + "仟" ) + ( MoneyStr.Substring( 3, 1 ) == "零" ? "零" : MoneyStr.Substring( 3, 1 ) + "佰" ) + ( MoneyStr.Substring( 4, 1 ) == "零" ? "零" : MoneyStr.Substring( 4, 1 ) + "拾" ) + ( MoneyStr.Substring( 5, 1 ) == "零" ? "" : MoneyStr.Substring( 5, 1 ) ) + "元";
                    break;
                default:
                    tempStr = "支持金额不能超来1,000,000元";
                    break;
            }

            if( MoneyValues.Length > 1 )
            {
                string strJiao = MoneyValues[ 1 ].Substring( 0, 1 );
                string strFun = MoneyValues[ 1 ].Substring( 1, 1 );

                switch( strJiao )
                {
                    case "1":
                        strJiao = "壹";
                        break;
                    case "2":
                        strJiao = "贰";
                        break;
                    case "3":
                        strJiao = "叁";
                        break;
                    case "4":
                        strJiao = "肆";
                        break;
                    case "5":
                        strJiao = "伍";
                        break;
                    case "6":
                        strJiao = "陆";
                        break;
                    case "7":
                        strJiao = "柒";
                        break;
                    case "8":
                        strJiao = "捌";
                        break;
                    case "9":
                        strJiao = "玖";
                        break;
                    default:
                        strJiao = "零";
                        break;
                }

                switch( strFun )
                {
                    case "1":
                        strFun = "壹";
                        break;
                    case "2":
                        strFun = "贰";
                        break;
                    case "3":
                        strFun = "叁";
                        break;
                    case "4":
                        strFun = "肆";
                        break;
                    case "5":
                        strFun = "伍";
                        break;
                    case "6":
                        strFun = "陆";
                        break;
                    case "7":
                        strFun = "柒";
                        break;
                    case "8":
                        strFun = "捌";
                        break;
                    case "9":
                        strFun = "玖";
                        break;
                    default:
                        strFun = "零";
                        break;
                }

                tempStr += strJiao + "角" + strFun + "分";
            }

            return ConverZero( tempStr );//调用CoverZero函数进一步处理成正确的大写格式


        }

        /// <summary>
        /// 实现将后面的零去掉,如贰仟零零元转换成贰仟元
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string ConverZero( string money )
        {
            while( money.IndexOf( "零零" ) != -1 )
            {
                money = money.Replace( "零零", "零" );
            }

            while( money.IndexOf( "零元" ) != -1 )
            {
                money = money.Replace( "零元", "元" );
            }

            return money;
        }

        #endregion

        /// <summary>
        /// 截取字符串 
        /// Writed by 王超
        /// </summary>
        /// <param name="strOriginal">原字符串</param>
        /// <param name="remainNum">截取长度(保留长度)</param>
        /// <returns></returns>
        public static string Substring( string strOriginal, int remainNum )
        {
            string temp = string.Empty;

            if( System.Text.Encoding.Default.GetByteCount( strOriginal ) <= remainNum )//如果长度比需要的长度n小,返回原字符串
            {
                return strOriginal;
            }
            else
            {
                int t = 0;
                char[] q = strOriginal.ToCharArray();
                for( int i = 0 ; i < q.Length && t < remainNum ; i++ )
                {
                    if( ( int )q[ i ] >= 0x4E00 && ( int )q[ i ] <= 0x9FA5 )//是否汉字
                    {
                        temp += q[ i ];
                        t += 2;
                    }
                    else
                    {
                        temp += q[ i ];
                        t++;
                    }
                }

                return ( temp + "..." );
            }
        }

        /// <summary> 
        /// 生成随机字符串 
        /// </summary> 
        /// <param name="len">随机数的长度</param> 
        /// <returns></returns> 
        public static string RandomString( int len )
        {
            string[] fh = new string[ ] { 
                "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "0" ,
                "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z" ,
                "A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" , "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "W" , "X" , "Y" , "Z"
            };

            Random rm = new Random( unchecked( ( int )DateTime.Now.Ticks ) );

            int wzc = 0;

            string filen = "";

            for( int i = 0 ; i < len ; i++ )
            {
                wzc = rm.Next( 0, fh.Length );

                filen = filen + fh[ wzc ];
            }

            return filen;
        }

        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>全角字符串</returns>
        ///<remarks>
        ///全角空格为12288，半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        ///</remarks>        
        public static string ToSBC( string input )
        {
            //半角转全角：
            char[] c = input.ToCharArray();

            for( int i = 0 ; i < c.Length ; i++ )
            {
                if( c[ i ] == 32 )
                {
                    c[ i ] = ( char )12288;
                    continue;
                }

                if( c[ i ] < 127 )
                    c[ i ] = ( char )( c[ i ] + 65248 );
            }

            return new string( c );
        }

        /// 转半角的函数(DBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        ///<remarks>
        ///全角空格为12288，半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        ///</remarks>
        public static string ToDBC( string input )
        {
            char[] c = input.ToCharArray();

            for( int i = 0 ; i < c.Length ; i++ )
            {
                if( c[ i ] == 12288 )
                {
                    c[ i ] = ( char )32;
                    continue;
                }

                if( c[ i ] > 65280 && c[ i ] < 65375 )
                    c[ i ] = ( char )( c[ i ] - 65248 );
            }

            return new string( c );
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StringToInt( object Expression, int defValue )
        {
            if( Expression != null )
            {
                string str = Expression.ToString();

                if( str.Length > 0 && str.Length <= 11 && Regex.IsMatch( str, @"^[-]?[0-9]*$" ) )
                {
                    if( ( str.Length < 10 ) || ( str.Length == 10 && str[ 0 ] == '1' ) || ( str.Length == 11 && str[ 0 ] == '-' && str[ 1 ] == '1' ) )
                    {
                        return Convert.ToInt32( str );
                    }
                }
            }

            return defValue;
        }

        /// <summary>
        /// 返回字符串真实长度, 1个汉字长度为2
        /// </summary>
        /// <returns></returns>
        public static int GetStringLength( string str )
        {
            return Encoding.Default.GetBytes( str ).Length;
        }

        /// <summary>
        /// 判断指定字符串在指定字符串数组中的位置

        /// </summary>
        /// <param name="strSearch">字符串</param>
        /// <param name="stringArray">字符串数组</param>
        /// <param name="caseInsensetive">是否不区分大小写, true为不区分, false为区分</param>
        /// <returns>字符串在指定字符串数组中的位置, 如不存在则返回-1</returns>
        public static int GetInArrayID( string strSearch, string[ ] stringArray, bool caseInsensetive )
        {
            for( int i = 0 ; i < stringArray.Length ; i++ )
            {
                if( caseInsensetive )
                {
                    if( strSearch.ToLower() == stringArray[ i ].ToLower() )
                    {
                        return i;
                    }
                }
                else
                {
                    if( strSearch == stringArray[ i ] )
                    {
                        return i;
                    }
                }

            }
            return -1;
        }

        /// <summary>
        /// 判断指定字符串是否属于指定字符串数组中的一个元素
        /// </summary>
        /// <param name="strSearch">字符串</param>
        /// <param name="stringArray">字符串数组</param>
        /// <param name="caseInsensetive">是否不区分大小写, true为不区分, false为区分</param>
        /// <returns>判断结果</returns>
        public static bool InArray( string strSearch, string[ ] stringArray, bool caseInsensetive )
        {
            return GetInArrayID( strSearch, stringArray, caseInsensetive ) >= 0;
        }

        /// <summary>
        /// 判断指定字符串是否属于指定字符串数组中的一个元素
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="stringarray">字符串数组</param>
        /// <returns>判断结果</returns>
        public static bool InArray( string str, string[ ] stringarray )
        {
            return InArray( str, stringarray, false );
        }

        /// <summary>
        /// 删除字符串尾部的回车/换行/空格
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string RTrim( string str )
        {
            for( int i = str.Length ; i >= 0 ; i-- )
            {
                if( str[ i ].Equals( " " ) || str[ i ].Equals( "\r" ) || str[ i ].Equals( "\n" ) )
                {
                    str.Remove( i, 1 );
                }
            }
            return str;
        }

        /// <summary>
        /// 生成指定数量的html空格符号
        /// </summary>
        public static string Spaces( int nSpaces )
        {
            StringBuilder sb = new StringBuilder();
            for( int i = 0 ; i < nSpaces ; i++ )
            {
                sb.Append( " &nbsp;&nbsp;" );
            }
            return sb.ToString();
        }

        /// <summary>
        /// 返回URL中结尾的文件名

        /// </summary>		
        public static string GetFilename( string url )
        {
            if( url == null )
            {
                return "";
            }
            string[] strs1 = url.Split( new char[ ] { '/' } );
            return strs1[ strs1.Length - 1 ].Split( new char[ ] { '?' } )[ 0 ];
        }

        /// <summary>
        /// 根据阿拉伯数字返回月份的名称(可更改为某种语言)
        /// </summary>	
        public static string[ ] Monthes
        {
            get
            {
                return new string[ ] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
            }
        }

        /// <summary>
        /// 替换回车换行符为html换行符

        /// </summary>
        public static string StrFormat2HTML( string str )
        {
            string str2;

            if( str == null )
            {
                str2 = "";
            }
            else
            {
                str = str.Replace( "\r\n", "<br />" );
                str = str.Replace( "\n", "<br />" );
                str2 = str;
            }
            return str2;
        }

        /// <summary>
        /// 格式化字节数字符串

        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string FormatBytesStr( int bytes )
        {
            if( bytes > 1073741824 )
            {
                return ( ( double )( bytes / 1073741824 ) ).ToString( "0" ) + "G";
            }
            if( bytes > 1048576 )
            {
                return ( ( double )( bytes / 1048576 ) ).ToString( "0" ) + "M";
            }
            if( bytes > 1024 )
            {
                return ( ( double )( bytes / 1024 ) ).ToString( "0" ) + "K";
            }
            return bytes.ToString() + "Bytes";
        }

        /// <summary>
        /// 移除Html标记
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RemoveHtml( string content )
        {
            string regexstr = @"<[^>]*>";
            return Regex.Replace( content, regexstr, string.Empty, RegexOptions.IgnoreCase );
        }

        /// <summary>
        /// 过滤HTML中的不安全标签
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RemoveUnsafeHtml( string content )
        {
            content = Regex.Replace( content, @"(\<|\s+)o([a-z]+\s?=)", "$1$2", RegexOptions.IgnoreCase );
            content = Regex.Replace( content, @"(script|frame|form|meta|behavior|style)([\s|:|>])+", "$1.$2", RegexOptions.IgnoreCase );
            return content;
        }

        /// <summary>
        /// 过滤HTML中的JS,VBS脚本
        /// </summary>
        /// <param name="content">过滤内容</param>
        /// <returns></returns>
        public static string RemoveUnsafeScript( string content )
        {
            content = Regex.Replace( content , @"/\<script[^>]*?>.*?\<\/script\>/is" , "" , RegexOptions.IgnoreCase );
            return content;
        }

        /// <summary>
        /// 从HTML中获取文本,保留br,p,img
        /// </summary>
        /// <param name="HTML"></param>
        /// <returns></returns>
        public static string GetTextFromHTML( string HTML )
        {
            System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex( @"</?(?!br|/?p|img)[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase );

            return regEx.Replace( HTML, "" );
        }

        /// <summary>
        /// 将全角数字转换为数字
        /// </summary>
        /// <param name="SBCCase"></param>
        /// <returns></returns>
        public static string SBCCaseToNumberic( string SBCCase )
        {
            char[] c = SBCCase.ToCharArray();
            for( int i = 0 ; i < c.Length ; i++ )
            {
                byte[] b = System.Text.Encoding.Unicode.GetBytes( c, i, 1 );
                if( b.Length == 2 )
                {
                    if( b[ 1 ] == 255 )
                    {
                        b[ 0 ] = ( byte )( b[ 0 ] + 32 );
                        b[ 1 ] = 0;
                        c[ i ] = System.Text.Encoding.Unicode.GetChars( b )[ 0 ];
                    }
                }
            }
            return new string( c );
        }

        /// <summary>
        /// 将字符串转换为Color
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static Color ToColor( string color )
        {
            int red , green , blue = 0;
            char[] rgb;
            color = color.TrimStart( '#' );
            color = Regex.Replace( color.ToLower(), "[g-zG-Z]", "" );
            switch( color.Length )
            {
                case 3:
                    rgb = color.ToCharArray();
                    red = Convert.ToInt32( rgb[ 0 ].ToString() + rgb[ 0 ].ToString(), 16 );
                    green = Convert.ToInt32( rgb[ 1 ].ToString() + rgb[ 1 ].ToString(), 16 );
                    blue = Convert.ToInt32( rgb[ 2 ].ToString() + rgb[ 2 ].ToString(), 16 );
                    return Color.FromArgb( red, green, blue );
                case 6:
                    rgb = color.ToCharArray();
                    red = Convert.ToInt32( rgb[ 0 ].ToString() + rgb[ 1 ].ToString(), 16 );
                    green = Convert.ToInt32( rgb[ 2 ].ToString() + rgb[ 3 ].ToString(), 16 );
                    blue = Convert.ToInt32( rgb[ 4 ].ToString() + rgb[ 5 ].ToString(), 16 );
                    return Color.FromArgb( red, green, blue );
                default:
                    return Color.FromName( color );

            }
        }

        /// <summary>
        /// 分割字符串

        /// </summary>
        public static string[ ] SplitString( string strContent, string strSplit )
        {
            if( strContent.IndexOf( strSplit ) < 0 )
            {
                string[] tmp = { strContent };
                return tmp;
            }

            return Regex.Split( strContent, Regex.Escape( strSplit ), RegexOptions.IgnoreCase );
        }

        /// <summary>
        /// 分割字符串

        /// </summary>
        /// <returns></returns>
        public static string[ ] SplitString( string strContent, string strSplit, int p_3 )
        {
            string[] result = new string[ p_3 ];

            string[] splited = SplitString( strContent, strSplit );

            for( int i = 0 ; i < p_3 ; i++ )
            {
                if( i < splited.Length )
                    result[ i ] = splited[ i ];
                else
                    result[ i ] = string.Empty;
            }

            return result;
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt( object Expression, int defValue )
        {

            if( Expression != null )
            {
                string str = Expression.ToString();
                if( str.Length > 0 && str.Length <= 11 && Regex.IsMatch( str, @"^[-]?[0-9]*$" ) )
                {
                    if( ( str.Length < 10 ) || ( str.Length == 10 && str[ 0 ] == '1' ) || ( str.Length == 11 && str[ 0 ] == '-' && str[ 1 ] == '1' ) )
                    {
                        return Convert.ToInt32( str );
                    }
                }
            }
            return defValue;
        }

        /// <summary>
        /// string型转换为float型

        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat( object strValue, float defValue )
        {
            if( ( strValue == null ) || ( strValue.ToString().Length > 10 ) )
            {
                return defValue;
            }

            float intValue = defValue;
            if( strValue != null )
            {
                bool IsFloat = Regex.IsMatch( strValue.ToString(), @"^([-]|[0-9])[0-9]*(\.\w*)?$" );
                if( IsFloat )
                {
                    intValue = Convert.ToSingle( strValue );
                }
            }
            return intValue;
        }

        /// <summary>
        /// 字符串如果操过指定长度则将超出的部分用指定字符串代替
        /// </summary>
        /// <param name="p_SrcString">要检查的字符串</param>
        /// <param name="p_Length">指定长度</param>
        /// <param name="p_TailString">用于替换的字符串</param>
        /// <returns>截取后的字符串</returns>
        public static string GetSubString( string p_SrcString, int p_Length, string p_TailString )
        {
            return GetSubString( p_SrcString, 0, p_Length, p_TailString );
        }

        /// <summary>
        /// 取指定长度的字符串

        /// </summary>
        /// <param name="p_SrcString">要检查的字符串</param>
        /// <param name="p_StartIndex">起始位置</param>
        /// <param name="p_Length">指定长度</param>
        /// <param name="p_TailString">用于替换的字符串</param>
        /// <returns>截取后的字符串</returns>
        public static string GetSubString( string p_SrcString, int p_StartIndex, int p_Length, string p_TailString )
        {
            string myResult = p_SrcString;

            //当是日文或韩文时(注:中文的范围:\u4e00 - \u9fa5, 日文在\u0800 - \u4e00, 韩文为\xAC00-\xD7A3)
            if( System.Text.RegularExpressions.Regex.IsMatch( p_SrcString, "[\u0800-\u4e00]+" ) ||
                System.Text.RegularExpressions.Regex.IsMatch( p_SrcString, "[\xAC00-\xD7A3]+" ) )
            {
                //当截取的起始位置超出字段串长度时
                if( p_StartIndex >= p_SrcString.Length )
                {
                    return "";
                }
                else
                {
                    return p_SrcString.Substring( p_StartIndex,
                                                   ( ( p_Length + p_StartIndex ) > p_SrcString.Length ) ? ( p_SrcString.Length - p_StartIndex ) : p_Length );
                }
            }


            if( p_Length >= 0 )
            {
                byte[] bsSrcString = Encoding.Default.GetBytes( p_SrcString );

                //当字符串长度大于起始位置
                if( bsSrcString.Length > p_StartIndex )
                {
                    int p_EndIndex = bsSrcString.Length;

                    //当要截取的长度在字符串的有效长度范围内

                    if( bsSrcString.Length > ( p_StartIndex + p_Length ) )
                    {
                        p_EndIndex = p_Length + p_StartIndex;
                    }
                    else
                    {   //当不在有效范围内时,只取到字符串的结尾


                        p_Length = bsSrcString.Length - p_StartIndex;
                        p_TailString = "";
                    }



                    int nRealLength = p_Length;
                    int[] anResultFlag = new int[ p_Length ];
                    byte[] bsResult = null;

                    int nFlag = 0;
                    for( int i = p_StartIndex ; i < p_EndIndex ; i++ )
                    {

                        if( bsSrcString[ i ] > 127 )
                        {
                            nFlag++;
                            if( nFlag == 3 )
                            {
                                nFlag = 1;
                            }
                        }
                        else
                        {
                            nFlag = 0;
                        }

                        anResultFlag[ i ] = nFlag;
                    }

                    if( ( bsSrcString[ p_EndIndex - 1 ] > 127 ) && ( anResultFlag[ p_Length - 1 ] == 1 ) )
                    {
                        nRealLength = p_Length + 1;
                    }

                    bsResult = new byte[ nRealLength ];

                    Array.Copy( bsSrcString, p_StartIndex, bsResult, 0, nRealLength );

                    myResult = Encoding.Default.GetString( bsResult );

                    myResult = myResult + p_TailString;
                }
            }

            return myResult;
        }

        /// <summary>
        /// 截取从startString开始（包括startString）到原字符串结尾的所有字符   
        /// </summary>
        /// <returns>
        ///     A string value...
        /// </returns>
        public static string GetSubString( string sourceString, string startString )
        {
            try
            {
                int index = sourceString.ToUpper().IndexOf( startString );
                if( index > 0 )
                {
                    return sourceString.Substring( index );
                }
                return sourceString;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 截取从startString开始（不包括startString）到原字符串结尾的所有字符   
        /// </summary>
        /// <returns>
        ///     A string value...
        /// </returns>
        public static string GetSubStringEnd( string sourceString, string startString )
        {
            try
            {
                int index = sourceString.ToUpper().IndexOf( startString );
                int startLen = startString.Length;

                if( index > 0 )
                {
                    return sourceString.Substring( index + startLen );
                }
                return sourceString;
            }
            catch
            {
                return "";
            }
        }

        public static string GetSubString( string sourceString, string beginRemovedString, string endRemovedString )
        {
            try
            {
                if( sourceString.IndexOf( beginRemovedString ) != 0 )
                    beginRemovedString = "";

                if( sourceString.LastIndexOf( endRemovedString, sourceString.Length - endRemovedString.Length ) < 0 )
                    endRemovedString = "";

                int startIndex = beginRemovedString.Length;
                int length = sourceString.Length - beginRemovedString.Length - endRemovedString.Length;
                if( length > 0 )
                {
                    return sourceString.Substring( startIndex, length );
                }
                return sourceString;
            }
            catch
            {
                return sourceString; ;
            }
        }

        /// <summary>
        /// 将十六进制字符串转换为二进制输出,十六进制为 0F 00 10等

        /// </summary>
        /// <param name="hexSrcString"></param>
        /// <returns></returns>
        public static byte[ ] HexStringToBinary( string HexString )
        {
            string[ ] tmp = HexString.Trim().Split( ' ' );

            byte[ ] buff = new byte[ tmp.Length ];

            for( int i = 0 ; i < buff.Length ; i++ )
            {
                buff[ i ] = Convert.ToByte( tmp[ i ], 16 );
            }
            return buff;
        }

        /// <summary>
        /// 将字符串转换为十六进制编码

        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string StringToHex( string str )
        {
            // Get the integral value of the character.
            int value = Convert.ToInt32( str );

            // Convert the decimal value to a hexadecimal value in string form.
            string hex = String.Format( "{0:X}", value );

            return hex;
        }

        /// <summary>
        /// 向字符串左边填充字符
        /// </summary>
        /// <param name="SourceString">源字符串</param>
        /// <param name="Chr">填充字符</param>
        /// <param name="TotalLength">需要填充后达到的长度</param>
        /// <returns></returns>
        public static string FillCharWithLeft( string SourceString, char Chr, int TotalLength )
        {
            while( SourceString.Length < TotalLength )
            {
                SourceString = Chr.ToString() + SourceString;
            }

            return SourceString;
        }

        /// <summary>
        /// 获取当前时间,并且转换为字符串
        /// </summary>
        /// <returns></returns>
        public static string GetNowString()
        {
            return DateTime.Now.Year.ToString()
                + FillCharWithLeft( DateTime.Now.Month.ToString(), '0', 2 )
                + FillCharWithLeft( DateTime.Now.Day.ToString(), '0', 2 )
                + FillCharWithLeft( DateTime.Now.Hour.ToString(), '0', 2 )
                + FillCharWithLeft( DateTime.Now.Minute.ToString(), '0', 2 )
                + FillCharWithLeft( DateTime.Now.Second.ToString(), '0', 2 );
        }

        /// <summary>
        /// 获取当前时间,并且转换为字符串
        /// </summary>
        /// <returns></returns>
        public static string GetNowTimeString()
        {
            return FillCharWithLeft( DateTime.Now.Hour.ToString(), '0', 2 )
                + FillCharWithLeft( DateTime.Now.Minute.ToString(), '0', 2 )
                + FillCharWithLeft( DateTime.Now.Second.ToString(), '0', 2 );
        }

        /// <summary>
        /// 获取当前时间,并且转换为字符串
        /// </summary>
        /// <returns></returns>
        public static string GetTimeString()
        {
            return FillCharWithLeft( DateTime.Now.Hour.ToString(), '0', 2 )
               + ":" + FillCharWithLeft( DateTime.Now.Minute.ToString(), '0', 2 );
        }

        /// <summary>
        /// 从字符串中的尾部删除指定的字符串
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="removedString"></param>
        /// <returns></returns>
        public static string Remove( string sourceString , string removedString )
        {
            try
            {
                if ( sourceString.IndexOf( removedString ) < 0 )
                    throw new Exception( "原字符串中不包含移除字符串！" );
                string result = sourceString;
                int lengthOfSourceString = sourceString.Length;
                int lengthOfRemovedString = removedString.Length;
                int startIndex = lengthOfSourceString - lengthOfRemovedString;
                string tempSubString = sourceString.Substring( startIndex );
                if ( tempSubString.ToUpper( ) == removedString.ToUpper( ) )
                {
                    result = sourceString.Remove( startIndex , lengthOfRemovedString );
                }
                return result;
            }
            catch
            {
                return sourceString;
            }
        }

        /// <summary>
        /// 获取拆分符右边的字符串

        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="splitChar"></param>
        /// <returns></returns>
        public static string RightSplit( string sourceString , char splitChar )
        {
            string result = null;
            string[] tempString = sourceString.Split( splitChar );
            if ( tempString.Length > 0 )
            {
                result = tempString[ tempString.Length - 1 ].ToString( );
            }
            return result;
        }

        /// <summary>
        /// 获取拆分符左边的字符串

        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="splitChar"></param>
        /// <returns></returns>
        public static string LeftSplit( string sourceString , char splitChar )
        {
            string result = null;
            string[] tempString = sourceString.Split( splitChar );
            if ( tempString.Length > 0 )
            {
                result = tempString[ 0 ].ToString( );
            }
            return result;
        }

        /// <summary>
        /// 去掉最后一个逗号
        /// </summary>
        /// <param name="origin"></param>
        /// <returns></returns>
        public static string DelLastComma( string origin )
        {
            if ( origin.IndexOf( "," ) == -1 )
            {
                return origin;
            }
            return origin.Substring( 0 , origin.LastIndexOf( "," ) );
        }

        /// <summary>
        /// 去掉最后一个字符串
        /// </summary>
        /// <param name="origin"></param>
        /// <returns></returns>
        public static string DelLastComma( string origin , string str )
        {
            if ( origin.IndexOf( str ) == -1 )
            {
                return origin;
            }
            return origin.Substring( 0 , origin.LastIndexOf( str ) );
        }

        /// <summary>
        /// 替换掉一些不合适的字符。
        /// </summary>
        /// <returns></returns>
        public static string ReplaceChar( string oldStr )
        {
            string newStr = "";
            newStr = oldStr.Replace( "'" , "''" );
            return newStr;
        }

        /// <summary>
        /// 格式化字符串长度，超出部分显示省略号,区分汉字跟字母。汉字2个字节，字母数字一个字节
        /// </summary>
        /// <param name="str">要格式化得字符串</param>
        /// <param name="n">截取得字符长度</param>
        /// <returns>返回截取后得字符串</returns>
        public static string stringformat( string str , int n )
        {
            string temp = string.Empty;
            if ( System.Text.Encoding.Default.GetByteCount( str ) <= n )//如果长度比需要的长度n小,返回原字符串
            {
                return str;
            }
            else
            {
                int t = 0;
                char[] q = str.ToCharArray( );
                for ( int i = 0 ; i < q.Length && t < n ; i++ )
                {
                    if ( ( int ) q[ i ] >= 0x4E00 && ( int ) q[ i ] <= 0x9FA5 )//是否汉字
                    {
                        temp += q[ i ];
                        t += 2;
                    }
                    else
                    {
                        temp += q[ i ];
                        t++;
                    }
                }
                return ( temp + "..." );
            }

        }

        /// <summary>
        /// XML内容特殊字符编码
        /// </summary>
        /// <param name="content">Xml内容</param>
        /// <returns></returns>
        public static string XmlEncode( string content )
        {
            content = content.Replace( "&" , "&amp;" );
            content = content.Replace( "<" , "&lt;" );
            content = content.Replace( ">" , "&gt;" );
            content = content.Replace( "'" , "&apos;" );
            content = content.Replace( "\"" , "&quot;" );

            return content;
        }

        /// <summary>
        /// XML内容特殊字符编码
        /// </summary>
        /// <param name="content">Xml内容</param>
        /// <returns></returns>
        public static string XmlDecode( string content )
        {
            content = content.Replace( "&lt;" , "<" );
            content = content.Replace( "&gt;" , ">" );
            content = content.Replace( "&apos;" , "'" );
            content = content.Replace( "&quot;" , "\"" );
            content = content.Replace( "&amp;" , "&" );

            return content;
        }


    }
}
